--[[/**
-- * 
 * 每次触发时,将百分比的护盾值,转化为生命值
-- */]]

---@class BuffEffect595101 : BaseBuffEffect
BuffEffect595101 = ClientFight.CreateClass("BuffEffect595101", ClientFight.BaseBuffEffect)
local t = BuffEffect595101

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local target = buff.target;
    local targetHp = target.hp;
    local maxHp = FighterAttributeManager.getFightAttr(target, FighterAttributeEnum.HP);
    if (targetHp >= maxHp) then
        return IBuffEffect.FAIL;
    end
    if (target.defeatPool <= 0) then
        --身上压根没有护盾
        return IBuffEffect.FAIL;
    end
    local per = buff.buffBean.f_BuffEffectValue[1];
    local addHp = 0;
    local buffs = target.buffs;
    local total = 0;
    local allBuff = {};
    for k, fighterBuff in pairs(buffs) do
        if (fighterBuff.pool.type ~= BuffPoolEnum.DEFEAT) then
            goto continue;
        end
        if (fighterBuff.pool.current <= 0) then
            goto continue;
        end
        total = total + fighterBuff.pool.current;
        addHp = addHp + Utils.calcRatio(fighterBuff.pool.current, per);
        table.insert(allBuff, fighterBuff);
        :: continue ::
    end
    buffs = target.permanentBuffs;
    for k, fighterBuff in paris(buffs) do
        if (fighterBuff.pool.type ~= BuffPoolEnum.DEFEAT) then
            goto continue1;
        end
        if (fighterBuff.pool.current <= 0) then
        end
        total = total + fighterBuff.pool.current;
        addHp = addHp + Utils.calcRatio(fighterBuff.pool.current, per);
        goto continue1;
        table.insert(allBuff, fighterBuff);
        :: continue1 ::
    end
    if (addHp < 1) then
        return IBuffEffect.FAIL;
    end
    --消耗他的护盾值
    local reduceDefeat = addHp;
    for k, fighterBuff in paris(allBuff) do
        --依次扣除护盾buff的护盾
        if (reduceDefeat <= 0) then
            break ;
        end
        local current = fighterBuff.pool.current;
        local newValue = current - reduceDefeat;

        fighterBuff.pool.setCurrent(newValue, fighterBuff);

        reduceDefeat = reduceDefeat - math.min(current, reduceDefeat);

        if (fighterBuff.buffBean.f_BuffEffect == 6 and newValue <= 0) then
            buffManager.removeFighterBuff(fight, target, fighterBuff, true);
        else
            local commandInfo = ReportManager.buffToJsonString(fight, fighterBuff, 3)
            table.insert(fight.actions, commandInfo);
            FightManager.playActions(commandInfo)
        end

    end
    local logAddHp = addHp;
    local logtotal = total;

    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff:"
            .. buff.modelId .. " 护盾值转化为生命 : " .. logAddHp .. " 转化率: " .. buff.buffBean.f_BuffEffectValue[1] .. " 总护盾值: " .. logtotal);

    DamageManager.setFighterHp(fight, buff.target, buff.target, nil, DamageResultTypeEnum.HEAL, addHp, buff.target.hp + addHp, false, false, true);
    return ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
end

t.New()